package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.map
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import me.leiusual.sharing.api.model.Invited
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class MineInvitedViewModel @Inject constructor(
    private val userRepository: UserRepository
) : ViewModel() {

  private val _invited = MutableLiveData<ParamsInvited>()
  private val _listing = map(_invited) {
    userRepository.invited(it.currentUserId, it.targetUserId)
  }
  val loadInitial: LiveData<Data<Invited.Response.User>> = switchMap(_listing) {
    it.loadInitial
  }
  val pagedList: LiveData<PagedList<Invited.Response.User>> = switchMap(_listing) {
    it.pagedList
  }

  fun invited(currentUserId: Long, targetUserId: Long) {
    _invited.value = ParamsInvited(currentUserId, targetUserId)
  }

  data class ParamsInvited(val currentUserId: Long, val targetUserId: Long)
}